/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package business.SEC;

import entity.SecMapUserRole;
import entity.SecRole;
import entity.SecUser;
import java.util.HashMap;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import session.ISecurityFacade;

/**
 *
 * @author nchanwut
 */
@Stateless
//@TransactionManagement(TransactionManagementType.CONTAINER)
public class SecLoginBusiness {
    // <editor-fold defaultstate="collapsed" desc="Param">

    @EJB
    ISecurityFacade iSecurityFacade;
    // </editor-fold>

    public Boolean DoRegister(String txtUsername, String txtPassword, String txtEmail) {
        // <editor-fold defaultstate="collapsed" desc="Param">
        Boolean blIsSuccess = false;
        SecUser secUser;
        List<SecRole> lSecRole;
        HashMap hmOptional;
        // </editor-fold>

        // <editor-fold defaultstate="collapsed" desc="Prepare Data">
        //เพิ่มผู้ใช้ใหม่
        secUser = new SecUser();
        secUser.setUsername(txtUsername);
        secUser.setPassword(txtPassword);
        secUser.setEmail(txtEmail);

        //ระบุว่า User นี้ มี Role อะไรบ้าง
        lSecRole = null;//(List<SecRole>) iSecurityFacade.GetDefaultRole();

        //optional arguement สำหรับเงื่อนไขการบันทึกข้อมูล
        hmOptional = new HashMap();
        hmOptional.put("IsSaveRole", true);
        hmOptional.put("SaveRoleType", "R");
        // </editor-fold>

        // <editor-fold defaultstate="collapsed" desc="Coding">
        try {
            blIsSuccess = iSecurityFacade.SaveSecUser(secUser, lSecRole, hmOptional);
            if (!blIsSuccess) {
                throw new Exception();
            }
        } catch (Exception e) {
        }

        // </editor-fold>

        // <editor-fold defaultstate="collapsed" desc="Clear Value">
        //secUser = null;
        //lSecRole.clear();
        //hmOptional.clear();
        // </editor-fold>

        return blIsSuccess;
    }
}
